package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 查询所有护理项目
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    public PageResponse<NursingProjectVo> findAll(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = (Page<NursingProjectVo>) nursingProjectMapper.findAll(name, status);
        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 添加护理项目
     * @param nursingProjectDto
     */
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto,NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setCreateBy(10L);
        nursingProject.setUpdateBy(10L);
        nursingProject.setStatus(0);

        nursingProjectMapper.add(nursingProject);
    }

    /**
     * 根据id查询护理项目
     * @param id
     * @return
     */
    public NursingProject getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        return nursingProject;
    }

    /**
     * 修改护理项目
     * @param nursingProjectDto
     */
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto,NursingProject.class);

        //根据id查询护理项目
        NursingProject nursingProject1 = nursingProjectMapper.getById(nursingProject.getId());
        //判断状态是否为0
        if(nursingProject1.getStatus()==1){
            throw new RuntimeException("该状态为开启中，不能修改");
        }
            nursingProject.setUpdateTime(LocalDateTime.now());
            nursingProjectMapper.update(nursingProject);

    }

    /**
     * 状态的启用禁用
     * @param id
     * @param status
     */
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    /**
     * 删除护理项目
     * @param id
     */
    public void delete(Long id) {
        //状态启用不能删除
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        if (nursingProject.getStatus()==1) {
            throw new RuntimeException("状态启用中,不能删除!");
        }
        //根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        //如果护理计划包含护理项目，则护理项目不能删除
        if (nursingPlan!=null){
            throw new RuntimeException("该护理项目被使用，不能删除!");
        }
        //删除护理项目
        nursingProjectMapper.delete(id);
    }

    /**
     * 查询所有护理项目
     * @param nursingProject
     * @return
     */
    public List<NursingProjectVo> list(NursingProject nursingProject) {
        List<NursingProjectVo> nursingProjectVoList = nursingProjectMapper.list(nursingProject);
        return nursingProjectVoList;
    }
}
